{ TOneSConnector class wrapper for COMconnector- http://www.1C.ru get mor info
  TOneSExternalConnection class wrapper for  ExternalConnection - http://www.1C.ru get mor info

  Copyright (C) 2012 leoniv.vlasov@gmail.com

  Module of part of project FPSOneSWrapper

  This source is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  A copy of the GNU General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.
}
unit OneSWrapperConnector;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,OleServer,comobj;
type
  {common types}
  TOneSVer = (OneS_V82,OneS_V81);
  { TOneSOleObject }

  TOneSOleObject = Class
    FOleObject:OLEVariant;
    constructor Create(AOleObject:OLEVariant);
    procedure RaiseExeption(E:Exception);
  end;

  TOneSExternalConnection  = Class ;
  TOneSWorkingProcessConnection  = Class;
  TOneSServerAgentConnection  = Class;
  { TOneSConnector }

  TOneSConnector = Class(TOneSOleObject)
  public
    constructor Create(Version:TOneSVer);
    function Connect(Const ConStr:WideString):TOneSExternalConnection;
    function ConnectAgent(Const ConStr:WideString):TOneSServerAgentConnection;
    function ConnectWorkingProcess(Const ConStr:WideString):TOneSWorkingProcessConnection;
  end;

  { TOneSCurentConnection }

  TOneSExternalConnection  = Class(TOneSOleObject)
    public
       constructor Create(AOleObject:OLEVariant);
       function NewObject(Name:WideString):OLEVariant;
       { TODO -oleo : метод 1с OneSExternalConnection.String() хер знает как }
  end;

{$I OneSWrapperWorkingProcessConnectionH.inc}
{$I OneSWrapperServerAgentConnectionH.inc}
implementation

{$I OneSWrapperWorkingProcessConnection.inc}
{$I OneSWrapperServerAgentConnection.inc}


{ TOneSCurentConnection }

constructor TOneSExternalConnection.Create(AOleObject: OLEVariant);
begin
   inherited Create(AOleObject);
end;

function TOneSExternalConnection.NewObject(Name: WideString): OLEVariant;
begin
   Result:=FOleObject.NewObject(Name);
end;

{ TOneSConnector }

constructor TOneSOleObject.Create(AOleObject: OLEVariant);
begin
  FOleObject:=AOleObject;
end;

procedure TOneSOleObject.RaiseExeption(E:Exception);
begin
 E.Message:=AnsiToUtf8(E.Message);
 Raise E;
end;

constructor TOneSConnector.Create(Version:TOneSVer);
begin
try
   case Version of
     OneS_V81:inherited Create(CreateOleObject('V81.COMConnector'));
     OneS_V82:inherited Create(CreateOleObject('V82.COMConnector'));
   end;
Except
   On E:Exception do
     RaiseExeption(E);
end;
end;

function TOneSConnector.Connect(const ConStr: WideString
  ): TOneSExternalConnection;
begin
  try
      Result:=TOneSExternalConnection.Create(FOleObject.Connect(ConStr));
  Except
     On E:Exception do
       RaiseExeption(E);
  end;
end;

function TOneSConnector.ConnectAgent(const ConStr: WideString): TOneSServerAgentConnection;
begin
  try
      Result:=TOneSServerAgentConnection.Create(FOleObject.ConnectAgent(ConStr));
   Except
     On E:Exception do
       RaiseExeption(E);
  end;
end;

function TOneSConnector.ConnectWorkingProcess(const ConStr: WideString
  ): TOneSWorkingProcessConnection;
begin
 try
      Result:= TOneSWorkingProcessConnection.Create(FOleObject.ConnectWorkingProcess(ConStr));

  Except
     On E:Exception do
       RaiseExeption(E);
  end;
end;


end.

